﻿using System;
using System.Collections.Generic;

using System.Text;

namespace Rance.Battle
{
    class 侠客AI : AI
    {
        public override AIResult Excute(角色 角色, 队伍状况 队伍状况, AI技能MGT 技能mgt)
        {
            AIResult result = new AIResult();
            var 袖中剑 = 技能mgt.Get("袖中剑");
            var 袖中剑target = Get输出Target(ConvertSoloTarget(袖中剑.TargetList), 队伍状况);
            result.Action = 袖中剑.Name;
            result.TargetList.Add(袖中剑target);

            return result;
        }

        protected virtual 角色 Get输出Target(List<角色> targetList, 队伍状况 队伍状况)
        {
            //优先攻击"准备中"的敌人
            List<角色> tempList = targetList.FindAll(r => r.是否准备);
            if (tempList.Count == 1)
                return targetList[0];
            else if (tempList.Count > 1)
            {
                var value = 队伍状况.战场.AI乱数表.Random(0, tempList.Count);
                return tempList[value];
            }

            //撇除"步卒"敌人
            tempList.Clear();
            foreach (var item in targetList)
            {
                if (item.兵种.类型 == "步卒")
                    continue;

                tempList.Add(item);
            }

            //攻击攻击高,血少的目标
            if (tempList.Count != 0)  
                targetList = tempList;

            if (targetList.Count == 1)
                return targetList[0];
            else
            {
                targetList.Sort((v1, v2) => (v1.兵种.攻 * (1000m / v1.兵力) * v1.实际攻).CompareTo((v2.兵种.攻 * (1000m / v2.兵力) * v2.实际攻)));
                targetList.Reverse();

                return targetList[0];
            }
        }

        private static 侠客AI instance = new 侠客AI();
        public static 侠客AI GetInstance()
        {
            return instance;
        }
    }
}
